package com.cx.healthex.base.autoDoc.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.cx.healthex.base.autoDoc.utils.ControllerPdfUtil;
import com.cx.healthex.base.autoDoc.utils.FileUtil;
import com.cx.healthex.base.autoDoc.utils.ServicePdfUtil;
import com.cx.healthex.base.autoDoc.annotations.AutoDocType;

/**
 * 自动生成接口文档--工具类
 * 
 * @author cenxi
 * @date 2015-11-11
 */
public class AutoDocUtil {

	/**
	 * Controller层生成文档执行方法
	 * */
	public static void ControllerAutoDoc(Class<?> mainClazz,String savePath,String fileName){
		//获取所有类
		List<Map<String, String>> list = FileUtil.getFile(FileUtil.getFilePath(mainClazz));
		List<Class<?>> clazzes = new ArrayList<Class<?>>();
		if(list != null && list.size() > 0){
			for(Map<String, String> map : list){
				String classpath = map.get("classPath");
				
				System.out.println("classpath --> "+classpath);
				
				Class<?> clazz = null;
				try {
					if(classpath != null && !"".equals(classpath)){
						clazz = Class.forName(classpath);
					}
				} catch (Exception e) {
					System.out.println("反射出错类"+classpath);
				} catch (java.lang.ExceptionInInitializerError e) {
					System.out.println("反射出错类"+classpath);
				}
				
				if(clazz != null && clazz.getAnnotation(AutoDocType.class) != null){
					clazzes.add(clazz);
				}
			}
		}
		
		//导出文档
		if(clazzes != null && clazzes.size() > 0){
			ControllerPdfUtil.createPdf(savePath, fileName, clazzes);
		}
	}
	
	
	/**
	 * 微服务层生成接口文档执行方法
	 * */
	public static void ServiceAutoDoc(Class<?> mainClazz,String savePath,String fileName){
		//获取所有类
		List<Map<String, String>> list = FileUtil.getFile(FileUtil.getFilePath(mainClazz));
		List<Class<?>> clazzes = new ArrayList<Class<?>>();
		if(list != null && list.size() > 0){
			for(Map<String, String> map : list){
				String classpath = map.get("classPath");
				
				System.out.println("classpath --> "+classpath);
				
				Class<?> clazz = null;
				try {
					if(classpath != null && !"".equals(classpath)){
						clazz = Class.forName(classpath);
					}
				} catch (Exception e) {
					System.out.println("反射出错类"+classpath);
				} catch (java.lang.ExceptionInInitializerError e) {
					System.out.println("反射出错类"+classpath);
				}
				
				if(clazz != null && clazz.getAnnotation(AutoDocType.class) != null){
					clazzes.add(clazz);
				}
			}
		}
		
		//导出文档
		if(clazzes != null && clazzes.size() > 0){
			ServicePdfUtil.createPdf(savePath, fileName, clazzes);
		}
	}
}
